package com.situation.controller;

import com.situation.annotation.ControllerEndpoint;
import com.situation.pojo.DO.TbDepartment;
import com.situation.pojo.DO.TbMenu;
import com.situation.pojo.VO.MenuNodeVO;
import com.situation.pojo.VO.MenuVO;
import com.situation.service.impl.MenuServiceImpl;
import com.situation.util.ExcelUtil;
import com.situation.util.RespUtil;
//import com.wuwenze.poi.ExcelKit;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description:
 * @author: 羽程
 * @create: 2021-12-08 15:42
 **/
@RestController
@RequestMapping("/api/menu")
public class MenuController {
    @Autowired
    private MenuServiceImpl menuService;

    @ControllerEndpoint(exceptionMessage = "添加菜单失败",operation = "添加菜单")
    @RequestMapping("/add")
    @RequiresPermissions({"menu:add"})
    public RespUtil addMenu(MenuVO menuVO){
        return RespUtil.ok().setData(menuService.addMenu(menuVO));
    }

    @ControllerEndpoint(exceptionMessage = "删除菜单失败",operation = "删除菜单")
    @RequestMapping("/delete")
    @RequiresPermissions({"menu:delete"})
    public RespUtil deleteMenu(TbMenu tbMenu){
        return RespUtil.ok().setData(menuService.deleteMenu(tbMenu));
    }

    @RequestMapping("/edit")
    @RequiresPermissions({"menu:edit"})
    public RespUtil edit(TbMenu tbMenu){
        return RespUtil.ok().setData(menuService.findMenuById(tbMenu));
    }

    @ControllerEndpoint(exceptionMessage = "更新菜单失败",operation = "更新菜单")
    @RequestMapping("/update")
    @RequiresPermissions({"menu:update"})
    public RespUtil update(MenuVO menuVO){
        return RespUtil.ok().setData(menuService.update(menuVO));
    }

    @ControllerEndpoint(exceptionMessage = "导出菜单execl失败",operation = "导出菜单execl")
    @RequestMapping("/excel")
    @RequiresPermissions("menu:export")
    public void excel(HttpServletResponse response){
        List<TbMenu> allMenu = menuService.findAllMenu();
        List<List<String>> excelData = new ArrayList<>();
        List<String> headList=new ArrayList<>();
        headList.add("菜单按钮ID");
        headList.add("上级菜单ID");
        headList.add("菜单名称");
        headList.add("菜单URL地址");
        headList.add("权限标识");
        headList.add("图标");
        headList.add("类型 0菜单 1按钮");
        headList.add("排序");
        headList.add("创建时间");
        headList.add("修改时间");
        excelData.add(headList);

        for (TbMenu menu : allMenu) {
            List<String> list=new ArrayList<>();
            long id = menu.getId();
            int tmpid1=(int)id;
            long id1 = menu.getParentId();
            int tmpid2=(int)id1;
            list.add(Integer.toString(tmpid1));
            list.add(Integer.toString(tmpid2));
            list.add(menu.getMenuName());
            list.add(menu.getUrl());
            list.add(menu.getPerms());
            list.add(menu.getIcon());
            list.add(menu.getType().toString());
            long num = menu.getOrderNum();
            int tmpid3=(int)num;
            list.add(Integer.toString(tmpid3));
            list.add(menu.getCreateTime().toString());
            list.add(menu.getModifiedTime().toString());
            excelData.add(list);
        }
        String sheetName = "菜单列表";
        String fileName = "菜单信息统计表";
        try {
            ExcelUtil.exportExcel(response, excelData, sheetName, fileName, 15);
        } catch (IOException e) {
//            logger.info("导出失败");
            System.out.println("导出失败");
        }
    }

    @RequestMapping("/tree")
    public RespUtil tree(){
        List<MenuNodeVO> menuTree = menuService.findMenuTree();
        List<Long> ids = menuService.findOpenIds();
        Map<String,Object>map=new HashMap<>();
        map.put("tree",menuTree);
        map.put("open",ids);
        return RespUtil.ok().setData(map);
    }
}
